package net.sk.china.city.talk.service.impl.order;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sk.china.city.talk.dao.order.StoreOrderInfoDao;
import net.sk.china.city.talk.model.order.StoreOrderInfo;
import net.sk.china.city.talk.response.order.OrderInfoDetailResponse;
import net.sk.china.city.talk.response.order.StoreOrderInfoOldResponse;
import net.sk.china.city.talk.service.order.StoreOrderInfoService;
import net.sk.china.city.talk.service.product.StoreProductReplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/3 22:15
 * +----------------------------------------------------------------------
 */

@Service
public class StoreOrderInfoServiceImpl extends ServiceImpl<StoreOrderInfoDao, StoreOrderInfo> implements StoreOrderInfoService {

    @Resource
    private StoreOrderInfoDao dao;

    private final StoreProductReplyService storeProductReplyService;

    public StoreOrderInfoServiceImpl(StoreProductReplyService storeProductReplyService) {
        this.storeProductReplyService = storeProductReplyService;
    }

    /**
     * 根据id集合查询数据，返回 map
     * @param orderIdList id集合
     * @return {@code HashMap<Integer, List<StoreOrderInfoOldResponse>>}
     */
    @Override
    public HashMap<Integer, List<StoreOrderInfoOldResponse>> getMapInId(List<Integer> orderIdList) {
        HashMap<Integer, List<StoreOrderInfoOldResponse>> map = new HashMap<>(16);
        if(orderIdList.size() < 1){
            return map;
        }
        LambdaQueryWrapper<StoreOrderInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(StoreOrderInfo::getOrderId, orderIdList);
        List<StoreOrderInfo> systemStoreStaffList = dao.selectList(lambdaQueryWrapper);
        if(systemStoreStaffList.size() < 1){
            return map;
        }
        for (StoreOrderInfo storeOrderInfo : systemStoreStaffList) {
            //解析商品详情JSON
            StoreOrderInfoOldResponse StoreOrderInfoVo = new StoreOrderInfoOldResponse();
            BeanUtils.copyProperties(storeOrderInfo, StoreOrderInfoVo, "info");
            StoreOrderInfoVo.setInfo(JSON.parseObject(storeOrderInfo.getInfo(), OrderInfoDetailResponse.class));
            if(map.containsKey(storeOrderInfo.getOrderId())){
                map.get(storeOrderInfo.getOrderId()).add(StoreOrderInfoVo);
            }else{
                List<StoreOrderInfoOldResponse> storeOrderInfoVoList = new ArrayList<>();
                storeOrderInfoVoList.add(StoreOrderInfoVo);
                map.put(storeOrderInfo.getOrderId(), storeOrderInfoVoList);
            }
        }
        return map;
    }

    /**
     * 根据id集合查询数据，返回 map
     * @param orderId 订单号
     * @return {@code List<StoreOrderInfoOldResponse>}
     */
    @Override
    public List<StoreOrderInfoOldResponse> getOrderListByOrderId(Integer orderId) {
        LambdaQueryWrapper<StoreOrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreOrderInfo::getOrderId, orderId);
        List<StoreOrderInfo> systemStoreStaffList = dao.selectList(lambdaQueryWrapper);
        if(systemStoreStaffList.size() < 1){
            return null;
        }

        List<StoreOrderInfoOldResponse> storeOrderInfoVoList = new ArrayList<>();
        for (StoreOrderInfo storeOrderInfo : systemStoreStaffList) {
            //解析商品详情JSON
            StoreOrderInfoOldResponse storeOrderInfoVo = new StoreOrderInfoOldResponse();
            BeanUtils.copyProperties(storeOrderInfo, storeOrderInfoVo, "info");
            storeOrderInfoVo.setInfo(JSON.parseObject(storeOrderInfo.getInfo(), OrderInfoDetailResponse.class));
            storeOrderInfoVo.getInfo().setIsReply(storeProductReplyService.isReply(storeOrderInfoVo.getUnique(), storeOrderInfoVo.getOrderId()) ? 1 : 0);
            storeOrderInfoVoList.add(storeOrderInfoVo);
        }
        return storeOrderInfoVoList;
    }
}
